package com.psm.controller;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.psm.interceptor.IndexInterceptor;
import com.psm.model.*;
import com.psm.util.MD5;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by cheam on 15/6/19.
 */
@Before(IndexInterceptor.class)
public class DepartmentController extends BaseController<SysDepartment> {
    public Logger log=Logger.getLogger(getClass());

    public void index(){

        render("index.html");
    }


    public void listJson(){

        Record user = getCurrentUser();

        List<Object> param = new ArrayList<Object>();
        StringBuffer where = new StringBuffer();
        /** 添加查询字段条件*/
        String sortName = this.getPara("sort");
        String sortOrder = this.getPara("order", "desc");

        String dep_name = this.getPara("dep_name", "");
        String parent_id = this.getPara("parent_id", "");

        where.append(" and t.parent_id not in(0,1) and FIND_IN_SET(t.parent_id,getDepChildList("+user.get("dep")+"))");


        if(!"".equals(dep_name)){
            where.append(" and t.dep_name like '%"+dep_name+"%'");
        }
        if(!"".equals(parent_id)){
            where.append(" and ( FIND_IN_SET(t.id,getDepChildList("+parent_id+")) )");
        }

        if (sortName != null) {
            where.append(" order by ");
            where.append(sortName);
            where.append(" ");
            where.append(sortOrder);
        }

        Page<Record> p = Db.paginate(this.getParaToInt("page", 1), this.getParaToInt("rows", 20), "select t.*,tt.dep_name parent_name ", "from " + SysDepartment.tableName + " t," + SysDepartment.tableName + " tt where t.parent_id=tt.id  " + where.toString(), param.toArray());
        DataGrid dg = new DataGrid();
        dg.setRows(p.getList());
        dg.setTotal(p.getTotalRow());

        renderJson(dg);

    }

    public void edit(){
        try {
            Long id = getParaToLong("id");
            SysDepartment user = SysDepartment.me.findById(id);
            this.setAttr("po",user);
        } catch (Exception e) {
            log.error("编辑异常", e);
        }
    }
    public void view(){
        try {
            Long id = getParaToLong("id");
            SysDepartment user = SysDepartment.me.findById(id);
            this.setAttr("po",user);
        } catch (Exception e) {
            log.error("编辑异常", e);
        }
    }

    public void save(){
        String id = getPara("id","");
        String parent_id = getPara("parent_id","");
        String dep_name = getPara("dep_name","");
        String charge_person = getPara("charge_person","");
        String tel_phone = getPara("tel_phone","");
        String mobile_phone = getPara("mobile_phone","");
        String kw_charge_person = getPara("kw_charge_person","");
        String kw_tel_phone = getPara("kw_tel_phone","");
        String kw_mobile_phone = getPara("kw_mobile_phone","");
        String address = getPara("address","");
        String www_address = getPara("www_address","");
        String introduction = getPara("introduction","");
        String status = getPara("status","0");

        SysDepartment dep = null;
        if("".equals(id)){
            dep = new SysDepartment();
        }else{
            dep = SysDepartment.me.findById(id);
        }
        dep.set("parent_id",parent_id);
        dep.set("dep_name",dep_name);
        dep.set("charge_person",charge_person);
        dep.set("tel_phone",tel_phone);
        dep.set("mobile_phone",mobile_phone);
        dep.set("kw_charge_person",kw_charge_person);
        dep.set("kw_tel_phone",kw_tel_phone);
        dep.set("kw_mobile_phone",kw_mobile_phone);
        dep.set("address",address);
        dep.set("www_address",www_address);
        dep.set("introduction",introduction);
        dep.set("status",status);

        Record user = getCurrentUser();


        if("".equals(id)){
            dep.set("create_user",user.get("id"));
            dep.set("create_datetime",new Date());
            dep.save();
        }else{
            dep.set("modify_user",user.get("id"));
            dep.set("modify_datetime",new Date());
            dep.update();
        }

        Record re = new Record();
        re.set("success",1);
        re.set("msg","操作成功");

        renderJson(re);
    }

    /**选择树*/
    public void combotree() {
        Record user = getCurrentUser();
        Record minPid = SysDepartment.me.queryMinPid(user.get("dep").toString());

        List<Record> dataList = SysDepartment.me.queryAll(user.get("dep").toString());
        List<TreeNode> list = new ArrayList<TreeNode>();
        TreeNode rootNode = new TreeNode();
        rootNode.setId(minPid.getLong("pid"));
        rootNode.setText("");
        rootNode.setIconCls("icon-home");
        rootNode.setCanChk(false);
        fillTree(dataList, rootNode,"cname",true,null);
//        list.add(rootNode);
        this.renderJson_(rootNode.getChildren());
    }

    public void add_type(){
        render("add_type.html");
    }

    public void edit_type(){
        try {
            Long id = getParaToLong("id");
            SysDepartment dep = SysDepartment.me.findById(id);
            this.setAttr("po",dep);
            render("add_type.html");
        } catch (Exception e) {
            log.error("编辑异常", e);
        }
    }

}
